package com.bdth.controller;

import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bdth.model.Department;
import com.bdth.service.IDepartmentService;
import com.bdth.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.ModelAndView;

import com.bdth.edit.MyEditor;
import com.bdth.utils.HtmlUtil;
import com.bdth.utils.URLUtils;

public class BaseController {

	public final static String SUCCESS = "success";

	public final static String MSG = "msg";

	public final static String DATA = "data";

	public final static String LOGOUT_FLAG = "logoutFlag";

	@Autowired
	private IDepartmentService departmentService;

	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
		binder.registerCustomEditor(int.class, new MyEditor());
	}

	/**
	 * 获取IP地址
	 *
	 * @param request
	 * @return
	 */
	public String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	/**
	 * 所有ActionMap 统一从这里获取
	 *
	 * @return
	 */
	public Map<String, Object> getRootMap() {
		Map<String, Object> rootMap = new HashMap<String, Object>();
		//添加url到 Map中
		//rootMap.putAll(URLUtils.getUrlMap());
		return rootMap;
	}

	public ModelAndView forword(String viewName, Map context) {
		return new ModelAndView(viewName, context);
	}

	public ModelAndView forword(String viewName) {
		return new ModelAndView(viewName);
	}

	public ModelAndView error(String errMsg) {
		return new ModelAndView("error");
	}

	/**
	 * 提示成功信息
	 *
	 * @param message
	 */
	public void sendSuccessMessage(HttpServletResponse response, String message) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put(SUCCESS, true);
		result.put(MSG, message);
		HtmlUtil.writerJson(response, result);
	}

	/**
	 * 提示失败信息
	 *
	 * @param message
	 */
	public void sendFailureMessage(HttpServletResponse response, String message) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put(SUCCESS, false);
		result.put(MSG, message);
		HtmlUtil.writerJson(response, result);
	}


	/**
	 * @param depIdInt
	 * @return
	 * @throws Exception
	 */
	public List<Integer> getDepIds(int depIdInt) throws Exception {
		List<Integer> depIntList = new ArrayList<>();
		depIntList = findChildDeps(depIntList, depIdInt);
		depIntList.add(depIdInt);
		return depIntList;
	}


	/**
	 * @param depIntList
	 * @param depIdInt
	 * @return
	 * @throws Exception
	 */
	public List<Integer> findChildDeps(List<Integer> depIntList, int depIdInt) throws Exception {
		List<Department> departmentList = departmentService.selectByPId(depIdInt);
		if (departmentList != null && departmentList.size() > 0) {
			for (Department d : departmentList) {
				depIntList.add(d.getId());
				findChildDeps(depIntList, d.getId());

			}
		}

		return depIntList;
	}

}
